/* **************************************************************************/
/*xxcipdetailrp.p -  Construction in progress  Report for JiuQI             */
/*COPYRIGHT WININ . INC ALL RIGHTS RESERVED. THIS IS AN UNPUBLISHED WORK.   */
/*V8:ConvertMode=Report                                                     */
/*V8:WebEnabled=Yes                                                         */
/*V8:RunMode=Character,Windows                                              */
/*EXTRACTING DATA FROM MFG/PRO                                              */
/****************************************************************************/
/* PROCEDURE NAME        : xxcipdetailrp.p                                  */
/* PROCEDURE TYPE        : Source Program for QRF Report                    */
/* DESCRIPTION           : Construction in progress  Report for JiuQI       */ 
/* INCLUDE FILES         :                                                  */
/* CALLED BY             :                                                  */
/* CALLED PROCEDURES     :                                                  */
/* PROJECT               : CIP Module                                       */
/****************************************************************************/
/****************************************************************************/
/* WININ-NBR:       DATE:28-Nov-23  CREATED BY: GAOYAN                      */ 
/*              																				 */
/****************************************************************************/

   {mfsubdirs.i}
   {{&US_BBI}mfdeclre.i}
   {{&US_BBI}gplabel.i}
   
   {com/qad/shell/report/dsReportRequest.i}
   {com/qad/shell/report/ttReportParameter.i}
   {com/qad/shell/report/ReportConstants.i}
   {com/qad/shell/report/reports/ReportBusinessHelper.i}
   {com/qad/shell/report/reports/ttCommentAndFunc.i}
   
   {{&US_BBI}gpfieldv.i}
   
   {com/qad/mfgpro/gpgenfld.i}
 
   
   /*============================================================================*/
   /* ****************************  Main routine  ****************************** */
   /*============================================================================*/
   
   define input  parameter runReport       as     logical           no-undo.
   define input  parameter reportHandle    as     handle            no-undo.
   define input  parameter dataset         for    dsReportRequest.
   
   define variable reportName       as     character                 no-undo.
   define variable vhDS             as     handle                    no-undo.
   define variable last_lang        as     character                 no-undo.
   define variable langdateformat   as     character                 no-undo.
   define variable tmp-msg          as     character                 no-undo.
   define variable lc_label01       as     character                 no-undo.
   define variable yearperiod1      as     integer                   no-undo.
   define variable yearperiod2      as     integer                   no-undo.
   define variable lastyearperiod   as     integer                   no-undo.
   define variable yearbeginperiod  as     integer                   no-undo.
   define variable lc_reccnt        as     integer                   no-undo.
   define variable lc_subreccnt     as     integer                   no-undo.
   define variable lc_dt1           as     date                      no-undo.
   define variable lc_dt2           as     date                      no-undo.

   define output parameter dataset-handle phReportResults.
   
   /*=============================================================================*/
   /********************* Input Criteria variable definitions**********************/
   /*=============================================================================*/
   define variable ip_apar         as character no-undo.
   define variable ip_entity1      as character no-undo.
   define variable ip_entity2      as character no-undo.
   define variable ip_custsupp1    as character no-undo.
   define variable ip_custsupp2    as character no-undo.
   define variable ip_year1        as integer   no-undo.
   define variable ip_year2        as integer   no-undo.
   define variable ip_peridod1     as integer   no-undo.
   define variable ip_peridod2     as integer   no-undo.
   define variable ip_rpttype      as character no-undo.   
  
   {com/qad/shell/report/reporting.i}

/*============================================================================*/
/* ****************************  TEMP TABLES  ******************************* */
/*============================================================================*/


define temp-table DataSourceReportSettings
field setting_name      as character
field setting_value     as character.

/* TEMP-TABLE DEFINITION BLOCK */
   define temp-table tt_cipdetail no-undo
      field tt_seq                  as integer
      field tt_subseq               as integer
      field tt_year                 like posting.PostingYear
      field tt_period               like posting.postingperiod
      field tt_yearperiod           like posting.postingyearperiod
      field tt_entity               like company.companycode
      field tt_cipdesc              as   character
      field tt_project              as   character
      field tt_budget               as   decimal 
      field tt_begin                as   date 
      field tt_end                  as   date 
      field tt_start                as   date 
      field tt_openingamt           as   decimal
      field tt_increaseamt          as   decimal 
      field tt_increaseamtother          as   decimal 
      field tt_completed            as   decimal
      field tt_paidamt              as   decimal
      field tt_faamt                as   decimal
      field tt_faamtother                as   decimal
      field tt_other                as   decimal
      field tt_endamt               as   decimal
      field tt_reason               as   character
      index idx1 tt_seq
      .
   define temp-table ttReportOptions no-undo
       field tt_entity                as character
       field tt_year                  like posting.postingyear
       field tt_period                like posting.postingperiod
       .  

   define dataset dsReportResults for tt_cipdetail, ttReportOptions , DataSourceReportSettings . 
   define buffer b_codemstr for code_mstr.
   define buffer b_xxcipddet for xxcipd_det.
       
   empty temp-table tt_cipdetail         no-error.
   empty temp-table ttReportOptions no-error.

   last_lang = global_user_lang.

   for first ttReportRequest 
   no-lock:
   
      reportName = ttReportRequest.reportProgramName.
      vhDS = dataset dsReportResults:handle.
      run FillMetaData in reportHandle(vhDS,"xxcipdetailrp.meta.xml").

      run OverrideMetaData.
   
      if runReport then do:
         run RunReport
            (output dataset-handle phReportResults).
      end.
   
   end.
   
   assign
      global_user_lang = last_lang
      langdateformat   = "" .

   procedure OverrideMetaData:
     
    
  
   
   end.


   procedure RunReport:
   
   yearperiod1    = 0.
   yearperiod1    = 0.
   lastyearperiod = 0.
   yearbeginperiod = 0.
   
   empty temp-table tt_cipdetail    no-error.
   empty temp-table ttReportOptions no-error.

   
   define output parameter dataset-handle phReportResults.

 
      define variable hReportQuery as handle.
      define variable h_ttReportOptions as handle no-undo.

      if ip_entity2     = hi_char then ip_entity2     = "". 


   
      for each ttFilterCondition no-lock 
         where ttFilterCondition.BufferName = "ttReportOptions":
      
        if ttFilterCondition.bufferField = "tt_year"  then
        do:
           if ttFilterCondition.filterOperator = "="  then  
	       assign ip_year1 = int(filterValue)
                 ip_year2 = ip_year1.
                 if ttFilterCondition.filterOperator = ">="  then  
	       assign ip_year1 = int(filterValue).
                
                 if ttFilterCondition.filterOperator = "<="  then  
	       assign ip_year2 = int(filterValue).
                 
        end. 
        else if ttFilterCondition.bufferField = "tt_period"  then
        do:
           if ttFilterCondition.filterOperator = "="  then  
	       assign ip_peridod1 = int(filterValue)
                 ip_peridod2 = ip_peridod1.
                 if ttFilterCondition.filterOperator = ">="  then  
	       assign ip_peridod1 = int(filterValue).
                
                 if ttFilterCondition.filterOperator = "<="  then  
	       assign ip_peridod2 = int(filterValue).
                 
        end. 
         else if ttFilterCondition.bufferField = "tt_entity" then
         do:
            if ttFilterCondition.filterOperator = "="  then  
	       assign 
		            ip_entity1 = filtervalue
                  ip_entity2 = filtervalue.
               
            
	         if ttFilterCondition.filterOperator = ">=" then
                  ip_entity1 = filtervalue.               

            if ttFilterCondition.filterOperator = "<=" then
               ip_entity2 = filtervalue.
	      
         end.
                                     

      end. /*for each ttFilterCondition no-lock where */
  
      
     
      if ip_entity2     = "" then ip_entity2     = hi_char.
    
   
     yearperiod1 = int(string(ip_year1,"9999") + string(ip_peridod1,"99")).
     yearperiod2 = int(string(ip_year2,"9999") + string(ip_peridod2,"99")).
     
     lastyearperiod = yearperiod2 - 1.
     if ip_peridod2 = 12 then 
     lastyearperiod = int(string(ip_year2 - 1,"9999") + "12").
     
     yearbeginperiod = int(string(ip_year2,"9999") + string("01")).

   lc_dt1 = today.
   lc_dt2 = today.
   
   find glc_cal where glc_domain = global_domain
                  and glc_year   = int(ip_year1)
				  and glc_per    = int(ip_peridod1)
				  no-lock no-error.
	if available glc_cal then
    lc_dt1 = glc_start.
   find glc_cal where glc_domain = global_domain
                  and glc_year   = int(ip_year1)
				  and glc_per    = int(ip_peridod2)
				  no-lock no-error.	
	if available glc_cal then
    lc_dt2 = glc_end.

   lc_label01 = gettermlabel("Total",20).
   
      /*OUTPUT data preparation*/
      lc_reccnt = 0.
       lc_reccnt = lc_reccnt + 1.  
                create tt_cipdetail.
                 assign tt_cipdetail.tt_seq            = lc_reccnt
                        tt_cipdetail.tt_subseq         = lc_subreccnt
                        tt_cipdetail.tt_year           = ip_year1
                        tt_cipdetail.tt_period         = ip_peridod2  
                        tt_cipdetail.tt_entity         = ""
                        tt_cipdetail.tt_cipdesc        = lc_label01
                        tt_cipdetail.tt_project        = ""
                        tt_cipdetail.tt_budget         = 0
                        tt_cipdetail.tt_begin          = ?
                        tt_cipdetail.tt_end            = ?
                        tt_cipdetail.tt_start          = ?
                        tt_cipdetail.tt_openingamt     = 0
                        tt_cipdetail.tt_increaseamt   = 0
                        tt_cipdetail.tt_completed      = 0
                        tt_cipdetail.tt_paidamt        = 0
                        tt_cipdetail.tt_faamt          = 0
                        tt_cipdetail.tt_other          = 0
                        tt_cipdetail.tt_endamt         = 0
                        tt_cipdetail.tt_reason        = "".
      for each code_mstr no-lock where code_domain = global_domain
                                   and code_fldname = "xxciptypecat"
                                   .
       if code_mstr.code_value = "1" then 
       do:
           lc_reccnt = lc_reccnt + 1.  
                create tt_cipdetail.
                 assign tt_cipdetail.tt_seq            = lc_reccnt
                        tt_cipdetail.tt_subseq         = lc_subreccnt
                        tt_cipdetail.tt_year           = ip_year1
                        tt_cipdetail.tt_period         = ip_peridod2  
                        tt_cipdetail.tt_entity         = ""
                        tt_cipdetail.tt_cipdesc        = code_mstr.code_cmmt
                        tt_cipdetail.tt_project        = ""
                        tt_cipdetail.tt_budget         = 0
                        tt_cipdetail.tt_begin          = ?
                        tt_cipdetail.tt_end            = ?
                        tt_cipdetail.tt_start          = ?
                        tt_cipdetail.tt_openingamt     = 0
                        tt_cipdetail.tt_increaseamt   = 0
                        tt_cipdetail.tt_completed      = 0
                        tt_cipdetail.tt_paidamt        = 0
                        tt_cipdetail.tt_faamt          = 0
                        tt_cipdetail.tt_other          = 0
                        tt_cipdetail.tt_endamt         = 0
                        tt_cipdetail.tt_reason        = "".
           
           for each b_codemstr no-lock where b_codemstr.code_domain = global_domain
                                         and b_codemstr.code_fldname = "xxciptype"
                                         and b_codemstr.code_value  begins "U"
                                         .
                lc_subreccnt = 0.                         
                lc_reccnt = lc_reccnt + 1.  
                create tt_cipdetail.
                 assign tt_cipdetail.tt_seq            = lc_reccnt
                        tt_cipdetail.tt_subseq         = lc_subreccnt
                        tt_cipdetail.tt_year           = ip_year1
                        tt_cipdetail.tt_period         = ip_peridod2  
                        tt_cipdetail.tt_entity         = ""
                        tt_cipdetail.tt_cipdesc        = b_codemstr.code_cmmt
                        tt_cipdetail.tt_project        = ""
                        tt_cipdetail.tt_budget         = 0
                        tt_cipdetail.tt_begin          = ?
                        tt_cipdetail.tt_end            = ?
                        tt_cipdetail.tt_start          = ?
                        tt_cipdetail.tt_openingamt     = 0
                        tt_cipdetail.tt_increaseamt   = 0
                        tt_cipdetail.tt_completed      = 0
                        tt_cipdetail.tt_paidamt        = 0
                        tt_cipdetail.tt_faamt          = 0
                        tt_cipdetail.tt_other          = 0
                        tt_cipdetail.tt_endamt         = 0
                        tt_cipdetail.tt_reason        = "".
                        
               for each xxcip_mstr where xxcip_mstr.xxcip_domain = global_domain
                                     and xxcip_mstr.xxcip_type = b_codemstr.code_value
                                     and xxcip_mstr.xxcip_entity >= ip_entity1                                     
                                     and xxcip_mstr.xxcip_entity <= ip_entity2 
                                     no-lock.
                  
                   lc_reccnt = lc_reccnt + 1.                  
                   lc_subreccnt = lc_subreccnt + 1.                  
                                   
                 create tt_cipdetail.
                 assign tt_cipdetail.tt_seq            = lc_reccnt
                        tt_cipdetail.tt_subseq         = lc_subreccnt
                        tt_cipdetail.tt_year           = ip_year1
                        tt_cipdetail.tt_period         = ip_peridod2  
                        tt_cipdetail.tt_entity         = xxcip_entity
                        tt_cipdetail.tt_cipdesc        = xxcip_cipdesc
                        tt_cipdetail.tt_project        = xxcip_project
                        tt_cipdetail.tt_budget         = xxcip_budget_amt
                        tt_cipdetail.tt_begin          = xxcip_begin
                        tt_cipdetail.tt_end            = xxcip_end
                        tt_cipdetail.tt_start          = xxcip_start
                        tt_cipdetail.tt_openingamt     = 0
                        tt_cipdetail.tt_increaseamt   = 0
                        tt_cipdetail.tt_completed      = 0
                        tt_cipdetail.tt_paidamt        = 0
                        tt_cipdetail.tt_faamt          = 0
                        tt_cipdetail.tt_other          = 0
                        tt_cipdetail.tt_endamt         = 0
                        tt_cipdetail.tt_reason        = "".
                        
                        
                    /* Beginning Amount */    
                   for each xxcipd_det no-lock where xxcipd_det.xxcipd_domain = global_domain
                                                    and xxcipd_det.xxcipd_entity = xxcip_entity
                                                    and xxcipd_det.xxcipd_cipid  = xxcip_cipid
                                                    and xxcipd_det.xxcipd_effdate < lc_dt1
  
                                                    .
                        tt_cipdetail.tt_openingamt = tt_cipdetail.tt_openingamt + 
                                                 xxcipd_det.xxcipd_drin_amt -  
                                                 xxcipd_det.xxcipd_drout_amt .
                         for each b_xxcipddet no-lock where  b_xxcipddet.xxcipd_domain = global_domain
                                                    and b_xxcipddet.xxcipd_entity = xxcip_entity
                                                    and b_xxcipddet.xxcipd_cipid  = xxcip_cipid
                                                    and b_xxcipddet.xxcipd_effdate < lc_dt1
                                                    and b_xxcipddet.xxcipd_ciporgintrnbr = xxcipd_det.xxcipd_ciptrnbr.
  
                         tt_cipdetail.tt_increaseamt = tt_cipdetail.tt_increaseamt -        
                                                 b_xxcipddet.xxcipd_crin_amt +
                                                 b_xxcipddet.xxcipd_crout_amt .
                                                 
                       end.    /*  for each b_xxcipddet no-lock where  b_xxcipddet.xxcipd_domain = global_domain */                                                                       
                                                    
                     end.     /*  for each xxcipd_det no-lock where xxcipd_det.xxcipd_domain = global_domain */  
                    
                      for each xxcipd_det no-lock where xxcipd_det.xxcipd_domain = global_domain
                                                    and xxcipd_det.xxcipd_entity = xxcip_entity
                                                    and xxcipd_det.xxcipd_cipid  = xxcip_cipid
                                                    and xxcipd_det.xxcipd_effdate >= lc_dt1
                                                    and xxcipd_det.xxcipd_effdate <= lc_dt2
                                                   
                                                    .
                                                    
                          tt_cipdetail.tt_increaseamt = tt_cipdetail.tt_increaseamt +                        
                                                        xxcipd_det.xxcipd_drin_amt -  
                                                         xxcipd_det.xxcipd_drout_amt 
                                                         .
                          if xxcipd_det.xxcipd_datafrom begins "FA" then 
                           tt_cipdetail.tt_faamt =   tt_cipdetail.tt_faamt +    
                                                     xxcipd_det.xxcipd_crin_amt -  
                                                         xxcipd_det.xxcipd_crout_amt   .                         
                                                         
                                                         
                     end.     /*  for each xxcipd_det no-lock where xxcipd_det.xxcipd_domain = global_domain */   

                     tt_cipdetail.tt_endamt = tt_cipdetail.tt_openingamt +  tt_cipdetail.tt_increaseamt - tt_cipdetail.tt_faamt.
               end. /* for each xxcip_mstr where xxcip_mstr.xxcip_domain = global_domain */
                                     
           end. /* for each b_codemstr no-lock  */

       end. /*  if code_mstr.code_value = "1" then  */
      
      end.      /* for each code_mstr no-lock where code_domain = global_domain */
        
      
      
   IF lc_reccnt = 0 THEN 
   DO:       
      /*IF NO RECORDS*/
      {{&US_BBI}pxmsg.i &msgnum=1614 &ERRORLEVEL=3}  
   END.
  
   phReportResults = dataset dsReportResults:handle.
   delete object phReportResults no-error.
      
   end. /* procedure RunReport: */
